package com.example.lib_mvp.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.List;

public class RippleSearchView extends View {

    private static final float MIN_RADIUS = 60; // 最小波纹半径
    private static final int RIPPLE_COUNT = 4; // 波纹数量
    private static final float MAX_RADIUS = 260; // 最大波纹半径
    private static final int ANIMATION_DURATION = 4000; // 动画时长，单位毫秒
    private static final int START_COLOR = Color.parseColor("#7875FF"); // 起始颜色（蓝色）
    private static final int END_COLOR = Color.parseColor("#cccccc"); // 结束颜色（灰色）

    private List<Ripple> mRipples;
    private ValueAnimator[] mAnimators;
    private Paint paint;

    public RippleSearchView(Context context) {
        this(context, null);
    }

    public RippleSearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RippleSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        mRipples = new ArrayList<>();
        mAnimators = new ValueAnimator[RIPPLE_COUNT];

        for (int i = 0; i < RIPPLE_COUNT; i++) {
            Ripple ripple = new Ripple();
            mRipples.add(ripple);

            mAnimators[i] = ValueAnimator.ofFloat(MIN_RADIUS, MAX_RADIUS);
            mAnimators[i].setDuration(ANIMATION_DURATION);
            mAnimators[i].setInterpolator(new LinearInterpolator());

            mAnimators[i].setRepeatCount(ValueAnimator.INFINITE);
            mAnimators[i].setStartDelay(i * (ANIMATION_DURATION / RIPPLE_COUNT));
            final int index = i;
            mAnimators[i].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float radius = (float) animation.getAnimatedValue();
                    mRipples.get(index).radius = radius;
                    // 计算透明度，范围从 255 到 0
                    mRipples.get(index).alpha = (int) ((MAX_RADIUS - radius) / (MAX_RADIUS - MIN_RADIUS) * 255);
                    invalidate();
                }
            });
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        for (Ripple ripple : mRipples) {
            paint.setAlpha(ripple.alpha);
            // 根据半径计算颜色
            int color = calculateColor(ripple.radius);
            paint.setColor(color);
            canvas.drawCircle(centerX, centerY, ripple.radius, paint);
        }
    }

    /**
     * 根据半径计算颜色
     * @param radius 当前波纹的半径
     * @return 计算得到的颜色值
     */
    private int calculateColor(float radius) {
        float fraction = (radius - MIN_RADIUS) / (MAX_RADIUS - MIN_RADIUS);
        int startA = Color.alpha(START_COLOR);
        int startR = Color.red(START_COLOR);
        int startG = Color.green(START_COLOR);
        int startB = Color.blue(START_COLOR);

        int endA = Color.alpha(END_COLOR);
        int endR = Color.red(END_COLOR);
        int endG = Color.green(END_COLOR);
        int endB = Color.blue(END_COLOR);

        int currentA = (int) (startA + fraction * (endA - startA));
        int currentR = (int) (startR + fraction * (endR - startR));
        int currentG = (int) (startG + fraction * (endG - startG));
        int currentB = (int) (startB + fraction * (endB - startB));

        return Color.argb(currentA, currentR, currentG, currentB);
    }

    /**
     * 开始波纹动画
     */
    public void startRippleAnimation() {
        for (ValueAnimator animator : mAnimators) {
            if (!animator.isRunning()) {
                animator.start();
            }
        }
    }

    /**
     * 停止波纹动画
     */
    public void stopRippleAnimation() {
        for (ValueAnimator animator : mAnimators) {
            if (animator.isRunning()) {
                animator.cancel();
            }
        }
        for (Ripple ripple : mRipples) {
            ripple.radius = 0;
            ripple.alpha = 255;
        }
        invalidate();
    }

    private static class Ripple {
        float radius;
        int alpha = 255;
    }
}